package com.china08.yxyapi.api.home.index.news;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.platform.YxyHomeNews;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeNewsRepository;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

/**
 * 首页--新闻详情查询
 * 
 */
@CrossOrigin
@RestController
public class IndexRecommendNewController extends YxyApiBaseController {
    @Autowired
    private YxyHomeNewsRepository yxyHomeNewsRepository;

    @ApiOperation(value = "首页咨询列表热门")
    @ResponseBody
    @RequestMapping(value = "/api_yw/home/index/news/recommend/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody(required = false) RepModel reqModel) {

        String schoolId = reqModel.schoolId;
        List<YxyHomeNews> notices = yxyHomeNewsRepository.findTop8ByEdbIdAndStatusOrderByPvDesc(schoolId, 1);
        List<RespModel> list = new ArrayList<IndexRecommendNewController.RespModel>();
        for (YxyHomeNews yxyHomeNews : notices) {
            RespModel model = new RespModel();
            model.id = yxyHomeNews.getId();
            model.title = yxyHomeNews.getTitle();
            model.createdDate = DateToolUtils.format(yxyHomeNews.getCreatedDate(), DateToolUtils.FORMAT_SHORT_YMD);
            model.createdBy = yxyHomeNews.getCreatedBy();
            list.add(model);
        }
        return list;
    }

    protected static class RepModel extends YxyApiReqModel {

        // 域名
        public String domain;
        // 学校id
        public String schoolId;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "域名找不到了，请刷新页面");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        // 新闻id
        public String id;
        // 标题
        public String title;
        // 创建时间
        public String createdDate;
        public String createdBy;
    }
}
